package com.example.reagent_management.ui.setting

import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import com.example.lib_base.base.BaseDBFragment
import com.example.lib_base.base.DataBindingConfig
import com.example.lib_base.event.ShareViewModel
import com.example.lib_base.utils.MMKVUtils
import com.example.lib_base.utils.clickWithTrigger
import com.example.lib_base.widget.InputTextManager
import com.example.reagent_management.BR
import com.example.reagent_management.R
import com.example.reagent_management.databinding.FragmentSettingBinding

const val TEMP1_REMIND = "TEMP1"
const val TEMP2_REMIND = "TEMP2"
const val TEMP3_REMIND = "TEMP3"
const val TEMP4_REMIND = "TEMP4"
const val DAY__REMIND = "DAY"
const val COUNT_REMIND = "COUNT"

class SettingFragment : BaseDBFragment() {
    private lateinit var mEvent: ShareViewModel
    private val close = MutableLiveData(false)

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_setting)
            .addBindingParam(BR.title, "设置提醒")
            .addBindingParam(BR.clickProxy, ClickProxy())
    }

    override fun initViewModel() {
        mEvent = getApplicationScopeViewModel(ShareViewModel::class.java)
    }

    override fun initViewOrObserve() {
        with(binding as FragmentSettingBinding) {
            var agoDay = MMKVUtils.getHelper().decodeInt(DAY__REMIND, 1)
            var agoCount = MMKVUtils.getHelper().decodeInt(COUNT_REMIND, 1)
            setText(num1, num, num3, num4, day, count)
            //温度
            button2.setOnClickListener {
                motionLayout.clearAnimation()
                motionLayout.transitionToState(R.id.end1)
                setText(num1, num, num3, num4, day, count)
            }
            InputTextManager.with(viewLifecycleOwner)
                .addView(num1)
                .addView(num)
                .addView(num3)
                .addView(num4)
                .setMain(ok1)
                .build()
            ok1.setOnClickListener {
                MMKVUtils.getHelper().encode(TEMP1_REMIND, num1.text.toString().toDouble())
                MMKVUtils.getHelper().encode(TEMP2_REMIND, num.text.toString().toDouble())
                MMKVUtils.getHelper().encode(TEMP3_REMIND, num3.text.toString().toDouble())
                MMKVUtils.getHelper().encode(TEMP4_REMIND, num4.text.toString().toDouble())
                motionLayout.transitionToState(R.id.start)
            }
            //天数
            button.setOnClickListener {
                motionLayout.clearAnimation()
                motionLayout.transitionToState(R.id.end2)
                setText(num1, num, num3, num4, day, count)
            }
            InputTextManager.with(viewLifecycleOwner)
                .addView(day)
                .setMain(ok2)
                .build()
            ok2.setOnClickListener {
                val day = day.text.toString().toInt()
                MMKVUtils.getHelper().encode(DAY__REMIND, day)
                motionLayout.transitionToState(R.id.start)
                if (day != agoDay) {
                    agoDay = day
                    mEvent.hasChangedRemind.value = true
                }
            }
            //库存
            button3.setOnClickListener {
                motionLayout.clearAnimation()
                motionLayout.transitionToState(R.id.end3)
                setText(num1, num, num3, num4, day, count)
            }
            InputTextManager.with(viewLifecycleOwner)
                .addView(count)
                .setMain(ok3)
                .build()
            ok3.setOnClickListener {
                val count = count.text.toString().toInt()
                MMKVUtils.getHelper().encode(COUNT_REMIND, count)
                motionLayout.transitionToState(R.id.start)
                if (count != agoCount) {
                    agoCount = count
                    mEvent.hasChangedRemind.value = true
                }
            }
            //转到历史提醒信息  点击红点消失
            imageFilterView.clickWithTrigger {
                mEvent.messageRemind.value = 0
                nav().navigate(R.id.action_mainFragment_to_message)
            }
            mEvent.messageRemind.observe(this@SettingFragment) {
                if (it <= 0 || it == null) {
                    notice.visibility = View.INVISIBLE
                } else {
                    notice.visibility = View.VISIBLE
                }
            }
            close.observe(this@SettingFragment) {
                if (it) {
                    setText(num1, num, num3, num4, day, count)
                }
            }
        }
    }

    private fun setText(
        num1: TextView,
        num: TextView,
        num3: TextView,
        num4: TextView,
        day: TextView,
        count: TextView
    ) {
        num1.text = MMKVUtils.getHelper().decodeDouble(TEMP1_REMIND, 25.0).toString()
        num.text = MMKVUtils.getHelper().decodeDouble(TEMP2_REMIND, 28.0).toString()
        num3.text = MMKVUtils.getHelper().decodeDouble(TEMP3_REMIND, 35.0).toString()
        num4.text = MMKVUtils.getHelper().decodeDouble(TEMP4_REMIND, 40.0).toString()
        day.text = MMKVUtils.getHelper().decodeInt(DAY__REMIND, 1).toString()
        count.text = MMKVUtils.getHelper().decodeInt(COUNT_REMIND, 1).toString()
    }

    inner class ClickProxy {

        fun clickClose(): View.OnClickListener {
            return View.OnClickListener {
                (binding as FragmentSettingBinding).apply {
                    motionLayout.clearAnimation()
                    motionLayout.transitionToState(R.id.start)
                    close.value = true
                }
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.e("SettingFragment", "onDestroyView: ")
    }
}